/*
* jlb
*/
package com.jlb.platform.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.jlb.entity.TransferType;
import com.jlb.platform.entity.Type;
import com.jlb.user.entity.UserDepositState;
import com.jlb.user.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 平台管理器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/platform")
@Slf4j
@Api(value = "平台管理器", description = "平台管理器")
public class PlatformCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private UserSV userSV;

    @Resource
    private UserMinerSV userMinerSV;

    @Resource
    private UserJlbSV userJlbSV;

    @Resource
    private UserOreSV userOreSV;

    @Resource
    private UserActiveSV userActiveSV;

    @Resource
    private UserDepositSV userDepositSV;

    /**
     * 统计首页数据：会员总数，今日新增，矿机出售量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计首页数据", notes = "统计首页数据")
    @GetMapping(value = "/count")
    @ResponseBody
    public BeanRet count() {
        JSONObject jsonObject = new JSONObject();
        //统计会员总量
        Map<String, Object> params = new HashedMap();
        int userTotal = userSV.count(params);
        jsonObject.put("userTotal", userTotal);
        //统计今日新增
        int todayTotal = userSV.countToday();
        jsonObject.put("userTodayTotal", todayTotal);
        //统计矿机出售数量
        int saleMinerTotal = userMinerSV.count(params);
        jsonObject.put("saleMinerTotal", saleMinerTotal);
        //统计用户激活币数量
        int userActivesTotal = userActiveSV.sum();
        jsonObject.put("userActivesTotal", userActivesTotal);
        //统计待提炼矿石总量
        Integer oreRefineTotal = userOreSV.countRefineOre();
        jsonObject.put("oreRefineTotal", oreRefineTotal);
        //统计用户矿石总量
        Integer oreTotal = userOreSV.countOre();
        jsonObject.put("oreTotal", oreTotal);
        //统计用户金兰宝总量
        Integer jlbTotal = userJlbSV.jlbTotal();
        jsonObject.put("jlbTotal", jlbTotal != null ? jlbTotal : 0);
        int depositTotal = userDepositSV.sumDepositTotla(UserDepositState.Returned.name());
        jsonObject.put("depositTotal", depositTotal);
        return BeanRet.create(true, "查询成功", jsonObject);
    }

    /**
     * 为用户充值余额
     */
    @ApiOperation(value = "为用户充值余额", notes = "为用户充值余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "数量", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "enum", paramType = "query")
    })
    @PutMapping(value = "/addBalance")
    @ResponseBody
    public BeanRet addBalance(String account, Integer num, Type type) {
        if(StringUtils.isEmpty(account) || num == null || type == null) {
            return BeanRet.create(false, "参数不能为空");
        }
        if(type == Type.Jlb) {
            userJlbSV.platUpdateJlb(account, num, TransferType.In);
        } else  if (type ==Type.Ore) {
            userOreSV.platUpdateOre(account, num, TransferType.In);
        } else if(type == Type.Active) {
            userActiveSV.platUpdateActives(account, num, TransferType.In);
        }
        return  BeanRet.create(true, "充值成功");
    }

    /**
     * 扣除用户余额
     */
    @ApiOperation(value = "扣除用户余额", notes = "扣除用户余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "数量", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "enum", paramType = "query")
    })
    @PutMapping(value = "/deductBalance")
    @ResponseBody
    public BeanRet deductBalance(String account, Integer num, Type type) {
        if(StringUtils.isEmpty(account) || num == null || type == null) {
            return BeanRet.create(false, "参数不能为空");
        }
        if(type == Type.Jlb) {
            userJlbSV.platUpdateJlb(account, num, TransferType.Out);
        } else  if (type ==Type.Ore) {
            userOreSV.platUpdateOre(account, num, TransferType.Out);
        } else if(type == Type.Active) {
            userActiveSV.platUpdateActives(account, num, TransferType.Out);
        }
        return  BeanRet.create(true, "扣除成功");
    }

    /**
     * 查询网络拓扑图
     */
    @ApiOperation(value = "查询网络拓扑图", notes = "查询网络拓扑图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", dataType = "java.lang.String", paramType = "query"),
    })
    @PutMapping(value = "/getNetwork")
    @ResponseBody
    public BeanRet getNetwork(String account) {

        return  null;
    }

}
